<?php

/*
	Class: Navigation

	About: Author
		Rich Joslin

	About: License
		<http://communit.as/docs/license>

	About: See Also
	 	- <Cts_Db_Table_Abstract>
*/
class Navigation extends Cts_Db_Table_Abstract {

	/* Group: Instance Variables */

	/*
		Variable: $_name
			The name of the table or view to interact with in the data source.
	*/
    protected $_name = 'default_navigation';

	/*
		Variable: $_primary
			The primary key of the table or view to interact with in the data source.
	*/
    protected $_primary = 'id';

	/*
		Variable: $role_id
	*/
	public $role_id;

	/* Group: Constructors */

	/*
		Constructor: Navigation
			Calls the parent's constructor and sets the private role_id variable.
			Role ID is required in order to create a Navigation object.

		Arguments:
			role_id - None of these methods will work without a role ID.
			config (optional) - Required by parent constructor
	*/
    function Navigation($role_id, $config = null) {
		$this->role_id = $role_id;
    	return parent::__construct($config);
    }

	/* Group: Instance Methods */

	/*
		Function: getNavTree
			Returns a full tree (very jagged multidimensional array) of all navigation items that apply to the given role.

		Returns: array
	*/
	function getNavTree() {
		$top_nav_items = $this->getNavArrayByParentId(0);
		return $this->buildRecursiveNav(array(), $top_nav_items);
	}

	/*
		Function: buildRecursiveNav
			This is a recursive function that iterates through the supplied array, figures out if there are children,
			and either continues parsing the children (recursively),
			or assigns the results to the supplied parent (if the current recursion is complete).

		Arguments:
			nav_items - An array of nav items as pulled from the database using <getNavArrayByParentId>.
			parent_item - The item to which to add children if any are found.

		Returns: An array of nav items along with any descendants they have (i.e.: a tree, or multidimensional array).
	*/
	private function buildRecursiveNav($parent_item, $nav_items) {
		foreach ($nav_items as $nav_item) {
			$child_items = $this->getNavArrayByParentId($nav_item['id']);
			if (!array_key_exists('id', $parent_item)) {
				$parent_item[$nav_item['short_name']] = $this->buildRecursiveNav($nav_item, $child_items);
			} else {
				$parent_item['children'][$nav_item['short_name']] = $this->buildRecursiveNav($nav_item, $child_items);
			}
		}
		return $parent_item;
	}

	/*
		Function: getNavArrayByParentId

		Arguments:
			parent_id - The ID of the nav item for which to get children.

		Returns: array of nav items (as pulled from default_navigation table)
	*/
	function getNavArrayByParentId($parent_id) {
		$nav_items = $this->fetchAll('parent_id = ' . $parent_id . ' and role_id = ' . $this->role_id, 'sort_order');
		if (!is_null($nav_items)) {			
			return $nav_items->toArray();
		} else {
			// return an empty array instead of null so a foreach on the result doesn't throw a warning
			return array();
		}
	}

	/*
		Function: hasChildren
			Determines if a nav item has children.

		Arguments:
			nav_id: The ID of the nav item for which to determine whether it has children.

		Returns: boolean
	*/
	function hasChildren($nav_id) {
		return count($this->getNavArrayByParentId($nav_id)) > 0;
	}

	/*
		Function: insertNavTree
			Takes a hierarchical array of link settings and adds them all to the default_navigation table in the database.
			See the the output of the <getNavTree> method to see the structure of the array that should be passed into this.
			Topmost items must have a parent short name specified, but none of the descendants should have parent short names (if they do, they will be ignored).
			The short name and role ID are used to identify the parent record (role ID is passed in when instantiating a Navigation object).
			Parent ID can vary from system to system, so we don't use parent ID to locate the parent.

		Arguments:
			tree - An array full of nav items.

		Returns: void
	*/
	function insertNavTreeArray(array $tree) {

		// TODO - FINISH THIS

		foreach ($tree as $key => $item) {
			$short_name = $key;
			$parent_short_name = $item['parent_short_name'];
			$link_text = $item['link_text'];
			$url = $item['url'];
			$sort_order = $item['sort_order'];
			$children = $item['children'];
			// turn this into a recursion
			foreach ($children as $item2) {
				echo("sdf<br />");
			}
		}
		die();
	}

	function insertNavTreeXml($tree) {
		// TODO - take the XML, turn it into an array, pass it to insertNavTreeArray
	}

	function insertNavTreeJson($tree) {
		// TODO - take the JSON, turn it into an array, pass it to insertNavTreeArray
	}

}
